﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HM.CS;
using HM.TESTCS;
using System.Threading;

namespace HM
{
    public partial class AppLogin : Form
    {
        //测试类
        public ApploginTest objTest;
       

        public AppLogin()
        {
            InitializeComponent();
            SetFormWithAndHeith(this);
            if (!getConfigInfo())
            {
                return;
            }

            objTest = new ApploginTest();
            FillListView(objTest.GetAcctInfo());
            if (this.lvAcct.Items.Count > 0)
            {
                TakeInfoToTextBoxs(0);
                lvAcct.Items[0].Selected = true;
            }
        }

        /// <summary>
        /// 根据单据对应的实体类填充单据内容
        /// </summary>
        /// <param name="act"></param>
        public void FillListView(DataTable dt)
        {
            try
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        //账套编号
                        string acctIndex;
                        //账套名称
                        string acctName;
                        //账套数据库
                        string acctDB;
                        //账套数据库ID
                        string aisID;

                        acctIndex = Convert.ToInt32(dr["index"]).ToString();
                        acctName = dr["acctName"].ToString();
                        acctDB = dr["acctDB"].ToString();
                        aisID = dr["acctKeyValue"].ToString();

                        ListViewItem lvi = new ListViewItem();
                        //首列赋值
                        lvi.Text = acctIndex;
                        lvi.ImageIndex = 0;
                        lvi.SubItems.Add(acctName);
                        lvi.SubItems.Add(acctDB);
                        lvi.SubItems.Add(aisID);

                        lvAcct.Items.Add(lvi);
                    }
                    //结束数据处理，UI界面一次性绘制。
                    lvAcct.EndUpdate();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("抱歉,错误的原因是:" + e.ToString());
            }
        }

        #region "原来的填充方法"
        /// <summary>
        /// 根据单据对应的实体类填充单据内容
        /// </summary>
        /// <param name="act"></param>
        //public void FillListView(K3Account entry)
        //{
        //    try
        //    {
        //        if (entry.entrys.Count > 0)
        //        {
        //            foreach (K3AccountEntry en in entry.entrys)
        //            {
        //                //账套编号
        //                string acctIndex;
        //                //账套名称
        //                string acctName;
        //                //账套数据库
        //                string acctDB;
        //                //账套数据库ID
        //                string aisID;

        //                acctIndex = en.index.ToString();
        //                acctName = en.acctName;
        //                acctDB = en.acctDB;
        //                aisID = en.acctKeyValue;

        //                ListViewItem lvi = new ListViewItem();
        //                //首列赋值
        //                lvi.Text = acctIndex;
        //                lvi.ImageIndex = 0;
        //                lvi.SubItems.Add(acctName);
        //                lvi.SubItems.Add(acctDB);
        //                lvi.SubItems.Add(aisID);

        //                lvAcct.Items.Add(lvi);
        //            }
        //            //结束数据处理，UI界面一次性绘制。
        //            lvAcct.EndUpdate();
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        MessageBox.Show("抱歉,错误的原因是:" + e.ToString());
        //    }
        //}

        #endregion
        private void TakeInfoToTextBoxs(int row)
        {
            //账套名称
            string actName;
            //数据库名次
            string accDB;
            //主键在ListView中的索引值
            int keyIndex;

            keyIndex = lvAcct.Columns.IndexOf(colActName);

            actName = lvAcct.Items[row].SubItems[keyIndex].Text.ToString();

            accDB = lvAcct.Items[row].SubItems[keyIndex].Text.ToString();

            if (!string.IsNullOrEmpty(actName))
            {
                this.txtActName.Text = actName;
            }
        }

        private void lvAcct_SelectedIndexChanged(object sender, EventArgs e)
        {
            int intSelectIndex;
            ListView.SelectedIndexCollection selectInfos = lvAcct.SelectedIndices;
            if (selectInfos.Count > 1)
            {
                MessageBox.Show("请选择单行记录");
                return;
            }
            else if (selectInfos.Count == 1)
            {
                //选择记录为1的时候才进行处理
                intSelectIndex = selectInfos[0];
                //将值赋给相应的控件
                TakeInfoToTextBoxs(intSelectIndex);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            
            //用户名
            string strUserName = "";
            //密码
            string strUserPwd = "";
            //账套数据库
            string strActDB = "";
            //验证返回信息
            string strcheckInfo = "";
            //是否通过校验,true为通过,false为不通过
            bool checkLogin;

            int keyIndex;
            int intSelectIndex;

            //确定选择必须是单行
            if (lvAcct.SelectedIndices.Count > 1)
            {
                MessageBox.Show("账套选择数不能超过1条,请确定");
                return;
            }


            

            if (!string.IsNullOrEmpty(txtUser.Text.Trim()))
            {
                strUserName = txtUser.Text.Trim();
            }
            else
            {
                MessageBox.Show("请输入用户名");
                txtUser.Focus();
                return;
            }



            

            //设置密码
            strUserPwd = txtPwd.Text.Trim();

            //记录账套信息
            K3CurrentAccount.strLoginID = strUserName;
            K3CurrentAccount.strPwd = strUserPwd;
            
            intSelectIndex = lvAcct.SelectedIndices[0];
            keyIndex = lvAcct.Columns.IndexOf(colActDB);
            K3CurrentAccount.acctDB = lvAcct.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvAcct.Columns.IndexOf(colActID);
            K3CurrentAccount.acctKeyValue = lvAcct.Items[intSelectIndex].SubItems[keyIndex].Text;
            keyIndex = lvAcct.Columns.IndexOf(colActName);
            K3CurrentAccount.acctName = lvAcct.Items[intSelectIndex].SubItems[keyIndex].Text;




            Cursor.Current = Cursors.WaitCursor;
            Thread TD = new Thread(showwaitfrm);
            this.btnOK.Enabled = false;
            TD.Start();

            try
            {
                checkLogin = objTest.CheckLogin(strUserName, strUserPwd, ref strcheckInfo);
                if (checkLogin)
                {
                    MainForm frm = new MainForm();
                    frm.Show();
                    this.Hide();

                }
                else
                {
                    MessageBox.Show("登录失败,原因:" + strcheckInfo);
                }

                TD.Abort();
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                TD.Abort();
                Cursor.Current = Cursors.Default;

                MessageBox.Show("登录失败,原因:" + ex.ToString());
            }
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            ServerSet frm = new ServerSet();
            frm.Show();
        }

        private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnOK_Click(sender, e);
            }
        }

        private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                txtPwd.Focus();
            }
        }

        private void AppLogin_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void AppLogin_Resize(object sender, EventArgs e)
        {
            //Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            //int width = ScreenArea.Width;
            //int height = ScreenArea.Height;
        }

        private bool getConfigInfo()
        {
            bool returnValue;
            CXmlClass xml = new CXmlClass();
            string sip;
            string sport;
            string sRepeat;
            returnValue = false;
            sip = "";
            sport = "";
            sRepeat = "0";


            DataTable dt = xml.ReadXML();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sip = dr["IP"].ToString();
                    sport = dr["Port"].ToString();
                    sRepeat = dr["Repeat"].ToString();
                    Common.sServerIP = sip;
                    Common.sServerPort = sport;
                    if (sRepeat == "1")
                    {
                        Common.bCodeRepeat = true;
                    }
                }

                if (string.IsNullOrEmpty(sip))
                {
                    returnValue = false;
                    MessageBox.Show("请设置服务器信息");
                }
                else
                {
                    returnValue = true;
                }
            }
            else
            {
                MessageBox.Show("请设置服务器信息");
            }
            return returnValue;
        }



        private void SetFormWithAndHeith(Form obj)
        { 
            Rectangle ScreenArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            int width = ScreenArea.Width;
            int height = ScreenArea.Height;
            obj.Width = width - 5;
            obj.Height = height - 5;
        }

        private void showwaitfrm()
        {
            ProgressForm frm = new ProgressForm();
            frm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Thread TD = new Thread(showwaitfrm);
            TD.Start();
            Thread.Sleep(5000);
            TD.Abort();
            Cursor.Current = Cursors.Default;
        }
    }
}